package yoyo.secret.app;

import yoyo.secret.app.db.DataBaseOperation;
import yoyo.secret.app.widget.CustomTextView;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

public class AddAccountFragment extends Fragment {
	private static DataBaseOperation mDataBaseOperation;
	private static String classification;
	public ImageView cancelAccountbtn, addAccountbtn;

	private OnNotify notify;

	public interface OnNotify {
		public void onNotify();
	}

	public void setOnNotify(OnNotify onNotify) {
		notify = onNotify;
	}

	static AddAccountFragment newInstance(DataBaseOperation operation, String classinfo) {
		AddAccountFragment fragment = new AddAccountFragment();
		mDataBaseOperation = operation;
		classification = classinfo;
		return fragment;
	}

	@Override
	public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {

		super.onInflate(activity, attrs, savedInstanceState);

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.add_account_fragment_layout, container, false);
		cancelAccountbtn = (ImageView) v.findViewById(R.id.cancel_account);
		addAccountbtn = (ImageView) v.findViewById(R.id.add_account);

		final CustomTextView nameEdittext = (CustomTextView) v.findViewById(R.id.class_name_edittext_id);
		final CustomTextView accountNameEdittext = (CustomTextView) v.findViewById(R.id.account_name_edittext_id);
		final CustomTextView passwordEdittext = (CustomTextView) v.findViewById(R.id.password_edittext_id);
		final CustomTextView markEdittext = (CustomTextView) v.findViewById(R.id.mark_edittext_id);

		cancelAccountbtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				nameEdittext.setEditTextVaule("");
				accountNameEdittext.setEditTextVaule("");
				passwordEdittext.setEditTextVaule("");
				markEdittext.setEditTextVaule("");
			}
		});

		addAccountbtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String accountName = accountNameEdittext.getEditTextVaule().toString();

				if (accountName.isEmpty()) {
					Toast.makeText(getActivity(), R.string.info_is_null_tips, Toast.LENGTH_SHORT).show();
				}

				long count = mDataBaseOperation.addUserInfo(accountNameEdittext.getEditTextVaule(), passwordEdittext.getEditTextVaule(),
						classification, markEdittext.getEditTextVaule(), nameEdittext.getEditTextVaule());
				Log.i("", "--------------------------------------->>>" + count);
				if (count != 0) {
					notify.onNotify();
				}

			}
		});

		return v;
	}
}
